<?php

/**
 * AuthRule(菜单管理)
 */
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class CateController extends AdminbaseController {
	function _initialize() {
		parent::_initialize ();
		
		$this->db = D ( "Cate" );
		$this->Models = F ( "Model" );
	}
	
	/**
	 * 显示菜单
	 */
	public function index() {
		$result = $this->db->order ( array (
				"listorder" => "DESC",
				"id" => "DESC" 
		) )->select ();
		import ( "Tree" );
		$tree = new \Tree ();
		$tree->icon = array (
				'&nbsp;&nbsp;&nbsp;│ ',
				'&nbsp;&nbsp;&nbsp;├─ ',
				'&nbsp;&nbsp;&nbsp;└─ ' 
		);
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		
		$newmenus = array ();
		foreach ( $result as $m ) {
			$newmenus [$m ['id']] = $m;
		}
		foreach ( $result as $n => $r ) {
			
			$result [$n] ['level'] = $this->_get_level ( $r ['id'], $newmenus );
			$result [$n] ['parentid_node'] = ($r ['parentid']) ? ' class="child-of-node-' . $r ['parentid'] . '"' : '';
			
			$result [$n] ['str_manage'] = '<a href="' . U ( $this->Models [$r ['model']] [edit_url], array (
					"id" => $r ['id'] 
			) ) . '">修改</a> | <a class="ajax-del"  href="' . U ( "delete", array (
					"id" => $r ['id'] 
			) ) . '">删除</a> ';
			
			$result [$n] ['model_name'] = $this->Models [$r ['model']] [name];
			
		
			$result [$n] ['ismenu'] = $r ['ismenu'] ? '<a href="' . U ( "change", array (
					"field" => "ismenu",
					"value" => "0",
					"id" => $r [id]
			) ) . '" class="ajax-switch"><i class="icon-check-circle"></i></a>' : '<a href="' . U ( "change", array (
					"field" => "ismenu",
					"value" => "1",
					"id" => $r [id]
			) ) . '" class="ajax-switch"><i class="icon-times-circle"></i></a>';
			
			
			$result [$n] ['status'] = $r ['status'] ? '<a href="' . U ( "change", array (
					"field" => "status",
					"value" => "0",
					"id" => $r [id] 
			) ) . '" class="ajax-switch"><i class="icon-check-circle"></i></a>' : '<a href="' . U ( "change", array (
					"field" => "status",
					"value" => "1",
					"id" => $r [id] 
			) ) . '" class="ajax-switch"><i class="icon-times-circle"></i></a>';
			
			$result [$n] ['url'] = $r ['url'] ? '<a href="'.$r[url].'" target="_black">访问</a>' : '<a href="' . U ( "cache" ) . '" class="ajax-get red">更新缓存</a>';
		
			
		}
		
		$tree->init ( $result );
		$str = "<tr id='node-\$id' \$parentid_node>
					<td><input name='listorders[\$id]' type='text' size='4' value='\$listorder' class='input input-order'></td>
					<td>\$id</td>
        			<td>\$spacer\$name</td>
					<td>\$model_name</td>
        		
        		<td>\$type</td>
        		<td>\$url</td>
        		<td>\$ismenu</td>
        		<td>\$status</td>
        		
				
					<td>\$str_manage</td>
				</tr>";
		$categorys = $tree->get_tree ( 0, $str );
		$this->assign ( "categorys", $categorys );
		$this->display ();
	}
	
	
	public function content_index(){
		$catid=cookie("list_cat");
		
		if($catid){
		$cates=F("Cates");
		$this_cate=$cates[$catid];
		$models=F("Model");
		$url=U($models[$this_cate[model]][info_list_url], array('catid' => $catid));
		}else{
			
			$url="";
		}
		
		$this->assign ( "this_url", $url );
		$this->assign ( "catid", $catid );
		
		$this->display ();
	}
	
	public  function content_list(){
		
		$json = array();
		$categorys = 	$result = $this->db->order ( array (
				"listorder" => "DESC",
				"id" => "DESC" 
		) )->select ();
		$models=F("Model");
		foreach ($categorys as $key=> $rs) {
		
			if ($rs['model'] == "Page"||$rs['model'] == "Link") {
				continue;
			}
		
			
			$data = array(
					'id' => $rs['id'],
					'parentid' => $rs['parentid'],
					'name' => $rs['name'],
					'type' => $rs['type'],
			);
			//终极栏目
			if ($rs['type'] ==2) {
				$data['target'] = 'right';
				$data['url'] = U($models[$rs[model]][info_list_url], array('catid' => $rs['id']));
				
				//设置图标
				//$data['icon'] = self::$Cache['Config']['siteurl'] . 'statics/js/zTree/zTreeStyle/img/diy/10.png';
			} else {
				$data['isParent'] = true;
			}
			$catid_check=cookie("list_cat");
			
			$cates=F("Cates");
		   
		    
		    
			if(in_array_string($rs['id'], $cates[$catid_check][arrparentid])){
				$data['open'] = true;
				
			}
			
			
			$json[] = $data;
		}
		$this->assign('json', json_encode($json));
		
		$this->display();
		
	}
	
	/**
	 * 获取菜单深度
	 * 
	 * @param
	 *        	$id
	 * @param
	 *        	$array
	 * @param
	 *        	$i
	 */
	protected function _get_level($id, $array = array(), $i = 0) {
		if ($array [$id] ['parentid'] == 0 || empty ( $array [$array [$id] ['parentid']] ) || $array [$id] ['parentid'] == $id) {
			return $i;
		} else {
			$i ++;
			return $this->_get_level ( $array [$id] ['parentid'], $array, $i );
		}
	}
	public function lists() {
		$result = $this->db->where ( array (
				"module" => $this->module 
		) )->order ( array (
				"name" => "ASC" 
		) )->select ();
		$this->assign ( "menus", $result );
		$this->display ();
	}
	
	/**
	 * 添加
	 */
	public function add() {
		if (IS_POST) {
			
		if(!$_POST[catdir]){
				unset($_POST[catdir]);
			}
			$_POST[letter]=pinyin($_POST[name]);
			if ($this->db->create ( $_POST )) {
				if ($this->db->add () !== false) {
					
					$this->success ( "添加成功！",U("index") );
				} else {
					$this->error ( "添加失败！" );
				}
			} else {
				$this->error ( $this->db->getError () );
			}
		} else {
			
			$model_id = I ( "get.model" );
			
			$this_model = $this->Models [$model_id];
			
			if (! $model_id) {
				$this->error ( "参数错误！" );
			}
			
			$this->assign ( "select_categorys", $this->db->get_admin_tree () );
			$tpl_cl = D ( "Model" )->select_tpls ( $model_id, "cl", $this_model ['tpl_cl'] );
			
			$tpl_list = D ( "Model" )->select_tpls ( $model_id, "list", $this_model ['tpl_list'] );
			$tpl_show = D ( "Model" )->select_tpls ( $model_id, "show", $this_model ['tpl_show'] );
			$this->assign ( "model_id", $model_id );
			$this->assign ( "tpl_cl", $tpl_cl );
			$this->assign ( "tpl_list", $tpl_list );
			$this->assign ( "tpl_show", $tpl_show );
			$this->display ( 'cate' );
		}
	}
	
	/**
	 * 编辑
	 */
	public function edit() {
		if (IS_POST) {
			if(!$_POST[catdir]){
				unset($_POST[catdir]);
			}
			$_POST[url]="";
			$_POST[thumb]=$_POST[thumb]?$_POST[thumb]:"";
			$_POST[letter]=pinyin($_POST[name]);
			if ($this->db->create ( $_POST )) {
				if ($this->db->save () !== false) {
					$this->success ( "更新成功！", U ( "index" ) );
				} else {
					$this->error ( "更新失败！" );
				}
			} else {
				$this->error ( $this->db->getError () );
			}
		}
		else
		{
			
			$id = intval ( I ( "get.id" ) );
			$rs = $this->db->where ( array (
					"id" => $id 
			) )->find ();
			
				$tpl_cl = D ( "Model" )->select_tpls ( $rs[model], "cl", $rs ['tpl_cl'] );
			$tpl_list = D ( "Model" )->select_tpls ( $rs[model], "list", $rs ['tpl_list'] );
			$tpl_show = D ( "Model" )->select_tpls ( $rs[model], "show", $rs ['tpl_show'] );
			$this->assign ( "model_id", $rs ['model'] );
			$this->assign ( "tpl_cl", $tpl_cl );
			$this->assign ( "tpl_list", $tpl_list );
			$this->assign ( "tpl_show", $tpl_show );
			
			$this->assign ( "edit", true );
			$this->assign ( "data", $rs );
			$this->assign ( "select_categorys", $this->db->get_admin_tree ( $rs ['parentid'] ) );
			$this->display ( 'cate' );
		}
	}
	
	/**
	 * 添加单页
	 */
	public function add_page() {
		if (IS_POST) {
		
			
			$_POST[type]='1';
			$_POST[model]='Page';
			$_POST[letter]=pinyin($_POST[name]);
			if ($this->db->create ( $_POST )) {
				if ($this->db->add () !== false) {
					
					$this->success ( "添加成功！", U ( "index" ) );
				} else {
					$this->error ( "添加失败！" );
				}
			} else {
				$this->error ( $this->db->getError () );
			}
		} else {
			
			$this->assign ( "select_categorys", $this->db->get_admin_tree () );
			
			$tpl_list = D ( "Model" )->select_tpls ( "Page", "page", $this->Model ['tpl_cl'] );
			
			$this->assign ( "tpl_cl", $tpl_list );
			
			$this->display ( 'Page' );
		}
	}
	/**
	 * 编辑单页
	 */
	public function edit_page() {
		if (IS_POST) {
			$_POST[type]='1';
			$_POST[model]='Page';
			$_POST[url]="";
			$_POST[letter]=pinyin($_POST[name]);
			$_POST[thumb]=$_POST[thumb]?$_POST[thumb]:"";
			if ($this->db->create ( $_POST )) {
				if ($this->db->save () !== false) {
					$this->success ( "更新成功！", U ( "index" ) );
				} else {
					$this->error ( "更新失败！" );
				}
			} else {
				$this->error ( $this->db->getError () );
			}
		}
		else
		{
			
			$id = intval ( I ( "get.id" ) );
			$rs = $this->db->where ( array (
					"id" => $id 
			) )->find ();
			
			$tpl_list = D ( "Model" )->select_tpls ( "Page", "page", $rs ['tpl_cl'] );
			
			// echo $this->module;
			
			$this->assign ( "edit", true );
			$this->assign ( "tpl_cl", $tpl_list );
			
			$this->assign ( "select_categorys", $this->db->get_admin_tree ( $rs ['parentid'] ) );
			
			$this->assign ( "data", $rs );
			$this->display ( "Page" );
		}
	}
	
	
	/**
	 * 添加单页
	 */
	public function add_link() {
		if (IS_POST) {
		
			$_POST[model]='Link';
			if ($this->db->create ( $_POST )) {
				if ($this->db->add () !== false) {
						
					$this->success ( "添加成功！", U ( "Cate/index" ) );
				} else {
					$this->error ( "添加失败！" );
				}
			} else {
				$this->error ( $this->db->getError () );
			}
		} else {
				
			$this->assign ( "select_categorys", $this->db->get_admin_tree () );
				
	
				
	
				
			$this->display ( 'Link' );
		}
	}
	/**
	 * 编辑单页
	 */
	public function edit_link() {
		if (IS_POST) {
			$_POST[model]='Link';
			if ($this->db->create ( $_POST )) {
				if ($this->db->save () !== false) {
					$this->success ( "更新成功！", U ( "index" ) );
				} else {
					$this->error ( "更新失败！" );
				}
			} else {
				$this->error ( $this->db->getError () );
			}
		}
		else
		{
				
			$id = intval ( I ( "get.id" ) );
			$rs = $this->db->where ( array (
					"id" => $id
			) )->find ();
				
		
				
			// echo $this->module;
				
			$this->assign ( "edit", true );
		
				
			$this->assign ( "select_categorys", $this->db->get_admin_tree ( $rs ['parentid'] ) );
				
			$this->assign ( "data", $rs );
			$this->display ( "Link" );
		}
	}
	
	// 排序
	public function listorders() {
		$status = parent::_listorders ( $this->db );
		if ($status) {
			$this->success ( "排序更新成功！" );
		} else {
			$this->error ( "排序更新失败！" );
		}
	}
	/**
	 * 删除
	 */
	public function delete() {
		$id = intval ( I ( "get.id" ) );
		$count = $this->db->where ( array (
				"parentid" => $id 
		) )->count ();
		if ($count > 0) {
			$this->error ( "该菜单下还有子菜单，无法删除！" );
		}
		if ($this->db->delete ( $id ) !== false) {
			$this->success ( "删除菜单成功！" );
		} else {
			$this->error ( "删除失败！" );
		}
	}
	
	public function cache(){
		
		$this->db->data_cache();
		$this->db->category_cache();
		
		$this->success ( "更新成功！" );
	}
	
}

?>
